package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneAdditionalOfferInstService;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.MultipleChargeAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdFeeCache;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname MultipleChargeAttrLinkHandler
 * @Description 是否多方收费
 * @Author zhang.song
 * @Date 2022-01-30 16:05
 */
@Service
@AttrHandler(MultipleChargeAttrHandler.class)
public class MultipleChargeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneAdditionalOfferInstService operateSceneAdditionalOfferInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 是否多方收费=是，收费单位=展示，否则隐藏
        KiteAttr chargeUnit = inst.getAttr("210005009");
        if (Objects.nonNull(chargeUnit)) {
            // 收费省、收费地市
            KiteAttr chargeProv = inst.getAttr("210008007");
            KiteAttr chargeCity = inst.getAttr("210008008");

            String sceneInstId = data.getSceneInstId();
            // 是否为页面初始化渲染，是（T），跳过校验
            String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(sceneInstId, "isInitPageRenderFlag");
            boolean initFlag = KiteStringUtils.isEqual(isInitPageRenderFlag, "T");

            if ("1_0000106".equals(attr.getValue())) {
                if(initFlag) {
                    chargeUnit.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    chargeProv.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    chargeCity.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                } else {
                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, chargeUnit, KeyConsts.PROD_ATTR_SHOW_TYPE_D, chargeUnit.getValue());
                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, chargeProv, KeyConsts.PROD_ATTR_SHOW_TYPE_H, chargeProv.getValue());
                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, chargeCity, KeyConsts.PROD_ATTR_SHOW_TYPE_H, chargeCity.getValue());
                }
            } else {
                if(initFlag) {
                    chargeUnit.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    chargeProv.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    chargeCity.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                } else {
                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, chargeUnit, KeyConsts.PROD_ATTR_SHOW_TYPE_E, chargeUnit.getValue());
                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, chargeProv, KeyConsts.PROD_ATTR_SHOW_TYPE_E, chargeProv.getValue());
                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, chargeCity, KeyConsts.PROD_ATTR_SHOW_TYPE_E, chargeCity.getValue());

                    String custId = data.getCustId();
                    // 删除收费方资费
                    List<KiteOfferInst> offerInsts = querySceneInstService.listMainOfferInsts(sceneInstId);
                    offerInsts.forEach(offerInst -> {
                        List<KiteOfferInst> additionalOfferInsts = querySceneInstService.listAdditionalOfferInstsFromOfferInst(custId, offerInst.getOfferInstId());
                        additionalOfferInsts.forEach(additionalOfferInst -> {
                            Map<String, Object> offerMap = ProdFeeCache.get(additionalOfferInst.getOfferId()).getOfferMap();
                            String payType = KiteMapUtils.getString(offerMap, "payType");
                            if(KiteStringUtils.equalsAny(payType, "monthly-charge", "one-time-charge")) {
                                // 删除资费
                                operateSceneAdditionalOfferInstService.removeAdditionalOfferInst(sceneInstId, additionalOfferInst.getOfferInstId(), custId, null);
                            }
                        });
                    });
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
